package com.ruoyi.project.module.api.pay.common.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.project.module.common.dict.AppFeesetType;
import com.ruoyi.project.module.common.dict.AppMemberGrade;
import com.ruoyi.project.module.common.dict.AppOrderStatus;
import com.ruoyi.project.module.common.dict.AppOrderType;
import com.ruoyi.project.module.feeset.domain.Feeset;
import com.ruoyi.project.module.feeset.service.IFeesetService;
import com.ruoyi.project.module.member.domain.Member;
import com.ruoyi.project.module.member.service.IMemberService;
import com.ruoyi.project.module.order.domain.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class CommonPayService {

    @Autowired
    private IFeesetService feesetService;

    @Autowired
    private IMemberService memberService;

    /**
     * 创建通用订单
     * @param member
     * @param orderType
     * @param feesetId
     * @param point
     * @return
     */
    public Order createCommonOrder(Member member, String orderType, String feesetId, Integer point){
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setMemberName(member.getName());
        order.setOrderStatus(AppOrderStatus.D_0_WFK);
        order.setOrderTime(new Date());
        order.setOrderType(orderType);

        if(AppOrderType.D_0_CZHY.equals(orderType)){
            Assert.notBlank(feesetId,"没有选择套餐");
            order.setOrderSubject("充值会员-");
            Feeset feeset = feesetService.selectFeesetById(Long.valueOf(feesetId));
            order.setTotalPrice(feeset.getCurrentPrice());
            order.setOrderObject(feesetId);
        }else if(AppOrderType.D_1_GMDS.equals(orderType)){
            order.setOrderSubject("购买点数-");
            getTotalPriceByPoint(order,point);
            order.setOrderObject(point.toString());
        }else if(AppOrderType.D_2_XHDS.equals(orderType)){
            order.setOrderSubject("消耗点数-");
            getTotalPriceByPoint(order,point);
            order.setOrderObject(point.toString());
        }else if(AppOrderType.D_3_DZBG.equals(orderType)){
            order.setOrderSubject("定制报告-");
        }
        return order;
    }

    private void getTotalPriceByPoint(Order order, Integer count) {
        Assert.notNull(count,"积分不能为空");
        Feeset feeset = new Feeset();
        feeset.setFeesetType(AppFeesetType.D_2_JF);
        List<Feeset> feesetList = feesetService.selectFeesetList(feeset);
        Assert.notNull(count,"套餐不存在");
        order.setTotalPrice(feesetList.get(0).getCurrentPrice().multiply(new BigDecimal(count)));
    }

    /**
     * 通用处理-订单成功
     * @param order
     */
    @Transactional
    public void dealPaySuccess(Order order){
        LogUtils.ACCESS_LOG.info(order.toString());
        String orderType = order.getOrderType();
        if(AppOrderType.D_0_CZHY.equals(orderType)){
            String feesetId = order.getOrderObject();

            Feeset feeset = feesetService.selectFeesetById(Long.valueOf(feesetId));
            Integer validity = feeset.getValidity();

            Member member = memberService.selectMemberById(order.getMemberId());
            member.setGrade(AppMemberGrade.D_1_GJ);

            Date expire = member.getExpire();
            if(null==expire){
                expire = new Date();
            }
            DateTime newExpire = DateUtil.offset(expire, DateField.MONTH, validity);
            member.setExpire(newExpire);
            memberService.updateMember(member);
        }else if(AppOrderType.D_1_GMDS.equals(orderType)){
            String point = order.getOrderObject();
            Member member = getNowPoint(order);
            member.setPoints(member.getPoints() + Long.valueOf(point));
            memberService.updateMember(member);
        }else if(AppOrderType.D_2_XHDS.equals(orderType)){
            String point = order.getOrderObject();
            Member member = getNowPoint(order);
            member.setPoints(member.getPoints() - Long.valueOf(point));
            memberService.updateMember(member);
        }else if(AppOrderType.D_3_DZBG.equals(orderType)){

        }

    }

    private Member getNowPoint(Order order){
        Member member = memberService.selectMemberById(order.getMemberId());
        Long points = member.getPoints();
        if(null==points){
            member.setPoints(0L);
        }
        return member;
    }


}
